package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.util.DateUtils;
import com.softer.wxzj.entity.AccTransactionLog;
import com.softer.wxzj.service.AccTransactionLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @program: wxzj
 * @author: wxr
 * @create: 2019-08-26 10:51
 **/
@RestController
@RequestMapping("/accTransactionLog")
@Slf4j
public class AccTransactionLogController {
    @Autowired
    private AccTransactionLogService accTransactionLogService;

    /**
     * 查询明细
     *
     * @param page
     * @param houseId
     * @return
     */
    @RequestMapping("/selectLog")
    @LogRecord(description = "查询明细", operationUnit = LogOperationUnit.ACC_TRANSACTION_LOG, operationType = LogOperationType.SELECT)
    public PageInfo<AccTransactionLog> selectLog(Page page, String houseId) {
        return accTransactionLogService.selectLog(page, houseId);
    }

    /**
     * 查询明细
     *
     * @param page
     * @param busiNo
     * @return
     */
    @RequestMapping("/selectLogByBusiNo")
    @LogRecord(description = "查询明细", operationUnit = LogOperationUnit.ACC_TRANSACTION_LOG, operationType = LogOperationType.SELECT)
    public PageInfo<AccTransactionLog> selectLogByBusiNo(Page page, String busiNo, String buildName, String fuzzy) {
        return accTransactionLogService.selectLogByBusiNo(page, busiNo, buildName, fuzzy);
    }

    /**
     * 资金缴存统计  报表
     */
    @RequestMapping("/selectLogByBusiNoCell")
    @LogRecord(description = "资金缴存统计", operationUnit = LogOperationUnit.ACC_TRANSACTION_LOG, operationType = LogOperationType.SELECT)
    public void selectLogByBusiNoCell(@RequestBody AccTransactionLog accTransactionLog, HttpServletResponse response) {
        try {
            List<AccTransactionLog> companyList = accTransactionLogService.selectLogByBusiNoCell(accTransactionLog);
            HSSFWorkbook wb = new HSSFWorkbook();
//          工作簿名称
            HSSFSheet sheet = wb.createSheet("摘要");
//          合并单元格
//          表头
            HSSFRow row = null;
            row = sheet.createRow(0);
            //          设置行高
            row.setHeight((short) (30 * 20));
            row.createCell(0).setCellValue("自然幢名称");
            row.createCell(1).setCellValue("业主姓名");
            row.createCell(2).setCellValue("房屋地址");
            row.createCell(3).setCellValue("房屋售价(元)");
            row.createCell(4).setCellValue("交易日期");
            row.createCell(5).setCellValue("交易类型");
            row.createCell(6).setCellValue("交易金额(元)");
            //绑定数据
            for (int i = 0; i < companyList.size(); i++) {
                row = sheet.createRow(i + 1);
                AccTransactionLog company = companyList.get(i);
                row.createCell(0).setCellValue(company.getBuildName());
                row.createCell(1).setCellValue(company.getOwnerName());
                row.createCell(2).setCellValue(String.valueOf(company.getHouseSite()));
                row.createCell(3).setCellValue(String.valueOf(company.getPrice()));
                row.createCell(4).setCellValue(String.valueOf(company.getTallyDate()));
                row.createCell(5).setCellValue(String.valueOf(company.getBusiType()));
                row.createCell(6).setCellValue(String.valueOf(company.getPayAmount()));
            }
            String title = DateUtils.getNow() + "";
            String fileName = new String(title.getBytes("GB2312"), "ISO8859-1");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");
            OutputStream os = response.getOutputStream();
            //默认Excel名称
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            log.error("IOException错误信息:()", e.getMessage(), e);
        }
    }

}
